<?php
declare (strict_types = 1);

namespace app\command;

use app\scan_bag\service\V1_Way_1_0Service;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\facade\Db;
use app\admin\model\iot\IntegralOrder;
use think\facade\Log;

class SynIntegralByOrder extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('synintegralbyorder')
            ->setDescription('the synintegralbyorder command');
    }

    protected function execute(Input $input, Output $output)
    {
        $where =[];
        $where[]=['is_sync','=',0];
        $list =IntegralOrder::getList($where);

        $order_arr = array_column($list['data'],'order_no');
        var_dump($order_arr);
        // 指令输出
        $v1_order =Db::connect('iot')
            ->table('member_cost')
            ->field("m.sn,m.status")
            ->alias('m')
            ->where('m.sn','in',$order_arr)
            ->select()
            ->toArray();

        $success_arr =[];
        $fail_arr=[];
        $deal_arr =array_column($v1_order,'sn');
        foreach ($v1_order as $v1){
            if($v1['status']!=1){
                $fail_arr[]=$v1['sn'];
            }
        }
        //更新同步状态
        IntegralOrder::update(['is_sync'=>1],[["order_no",'in',$deal_arr]]);
        var_dump($list['data']);
        var_dump($fail_arr);
        foreach ($list['data'] as $v){
            foreach ($fail_arr as $v2){
                if($v['order_no']==$v2){
                    echo 1111;
                    var_dump($v);
                    //失败的补回积分
                    $params =[
                        'unionid'=>$v['unionid'],
                        'type'=>10,
                        'bonus_points'=>100,
                        'action'=>1
                    ];
                    (new V1_Way_1_0Service())->updateMyPoints($params);
                    log::write($v['order_no'],"订单失败补回积分");
                }
            }
        }



    }
}
